home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / nihcl-30.lha / nihcl-3.0 / errfac / errgen.c < prev    next >
C/C++ Source or Header  |  1990-05-16  |  7KB  |  253 lines

  1. /* errgen.c -- Error message organizer
  2.  
  3.     THIS SOFTWARE FITS THE DESCRIPTION IN THE U.S. COPYRIGHT ACT OF A
  4.     "UNITED STATES GOVERNMENT WORK".  IT WAS WRITTEN AS A PART OF THE
  5.     AUTHOR'S OFFICIAL DUTIES AS A GOVERNMENT EMPLOYEE.  THIS MEANS IT
  6.     CANNOT BE COPYRIGHTED.  THIS SOFTWARE IS FREELY AVAILABLE TO THE
  7.     PUBLIC FOR USE WITHOUT A COPYRIGHT NOTICE, AND THERE ARE NO
  8.     RESTRICTIONS ON ITS USE, NOW OR SUBSEQUENTLY.
  9.  
  10. Author:
  11.     S.M. Orlow
  12.     Systex,Inc.
  13.     Beltsville, MD 20705
  14.     301-474-0111
  15.     June, 1986
  16.  
  17. Contractor:
  18.     K. E. Gorlen
  19.     Bg. 12A, Rm. 2017
  20.     Computer Systems Laboratory
  21.     Division of Computer Research and Technology
  22.     National Institutes of Health
  23.     Bethesda, Maryland 20892
  24.     Phone: (301) 496-5363
  25.     uucp: uunet!nih-csl!kgorlen
  26.     Internet: kgorlen@alw.nih.gov
  27.  
  28. Function:
  29.  
  30. Organizes error messages for errlib functions.
  31.  
  32. $Log:    errgen.c,v $
  33.  * Revision 3.0  90/05/16  09:31:33  kgorlen
  34.  * Release for 1st edition.
  35.  * 
  36. */
  37.  
  38. static const char rcsid[] = "$Header: /afs/alw.nih.gov/unix/sun4_40c/usr/local/src/nihcl-3.0/share/errfac/RCS/errgen.c,v 3.0 90/05/16 09:31:33 kgorlen Rel $";
  39.  
  40. #include <string.h>
  41. #include <libc.h>
  42. #include <osfcn.h>
  43. #include <fcntl.h>
  44. #include "ErrFac.h"
  45. #include "gettok.h"
  46. #include "dofile.h"
  47. #include "errors.h"
  48.  
  49. static istream *cmdstrm;
  50. static ostream *hstrm, *cstrm;
  51. static char *cmdfile, *hfile, *cfile;
  52. static char* regdirname = "/etc";
  53. static ErrorFacility* errfacility;
  54. static char* basename;
  55. static char* longname;
  56. static char* shortname;
  57. static ErrorSpecs* errfac;
  58. static int severity;
  59. static int errorcount = 0;
  60. static new_severity = 0;
  61. static new_facility = 0;
  62.  
  63. /* make_filenames -- make errgen data file names */
  64. void make_filenames(char* argv) {
  65.      char* ext;
  66.  
  67.      basename = base_filename(argv);
  68.      ext = ext_filename(argv);
  69.      if ( strlen(ext) == 0 ) {
  70.        ext = new char[5];
  71.        strcpy(ext,".err");
  72.        };
  73.      cmdfile = new char[strlen(basename)+strlen(ext)+1];
  74.      strcpy(cmdfile,basename); strcat(cmdfile,ext);
  75.      hfile = new char[strlen(basename)+2+1];
  76.      strcpy(hfile,basename); strcat(hfile,".h");
  77.      cfile = new char[strlen(basename)+2+1];
  78.      strcpy(cfile,basename); strcat(cfile,".c");
  79.      delete ext;
  80. }/* end make_filenames */
  81.  
  82. void clean_filenames() {
  83.     delete basename;
  84.     delete cmdfile;
  85.     delete hfile;
  86.     delete cfile;
  87.     }
  88.  
  89. int fac_code(istream& istrm,char* facname) {
  90.     char buf[512];
  91.     while ( !istrm.eof() ) {
  92.       istrm >> buf;
  93.       if ( strcmp(buf,".define") == 0 ) { // check for facility name
  94.         istrm >> buf;
  95.         if ( strcmp(buf,facname) == 0 ) { // get facility code
  96.         istrm >> buf;
  97.         return atoi(buf);
  98.         };
  99.         };
  100.       };
  101.     return -1;
  102.     }
  103.  
  104. int facility_code(char* facname) {
  105.     char regfilename[512];
  106.     sprintf(regfilename,"%s/errgen_tab",regdirname);
  107.     istream* istrm = open_istream(regfilename);
  108.         int retval = fac_code(*istrm,facname);
  109.     delete istrm;
  110.     if ( retval != -1 ) return retval;
  111.  
  112.     sprintf(regfilename,"%s/errgen_tab.loc",regdirname);
  113.     istrm = open_istream(regfilename);
  114.         retval = fac_code(*istrm,facname);
  115.     delete istrm;
  116.     if ( retval != -1 ) return retval;
  117.     cerr << "facility: " << facname << " not found\n";
  118.     exit(1);
  119.     return -1;
  120.     }
  121.  
  122. int severity_code(char* codename) {
  123.    if ( strcmp(codename,"FATAL")   == 0 ) return FATAL;
  124.    if ( strcmp(codename,"ERROR")   == 0 ) return ERROR;
  125.    if ( strcmp(codename,"WARNING") == 0 ) return WARNING;
  126.    if ( strcmp(codename,"INFO")    == 0 ) return INFO;
  127.    if ( strcmp(codename,"INFORMATION") == 0  ) return INFORMATION;
  128.    if ( strcmp(codename,"SUCCESS") == 0 ) return SUCCESS;
  129.    }
  130.  
  131. #define BUF_SIZE 512
  132.  
  133. /* process_commands -- process commands in *.err file */
  134. void process_commands() {
  135. char* buf = new char[BUF_SIZE];
  136. char *token, *token1;
  137.  
  138.    while ( !( cmdstrm->eof() ) ) {
  139.      cmdstrm->get(buf,BUF_SIZE,'\n');
  140.      *cmdstrm >> ws;
  141. //     cerr << buf << "\n";
  142.  
  143.      switch(buf[0]) {
  144.        case ';':
  145.          break;            // do nothing for comment 
  146.        case '.':
  147.            cerr << buf << "\n";
  148.               token = gettok(buf,white_space);
  149.        if ( strcmp(token,".facility") == 0 ) { // start new facility 
  150.           new_facility = 1;                         
  151.               new_severity = 0;                           
  152.           token = 0;
  153.           token = gettok(0,white_space);
  154.           if ( token ) {
  155.             longname = new char[strlen(token)+1]; 
  156.             strcpy(longname,token);
  157.         }
  158.            else {
  159.             cerr << " Missing longname for .facility\n";
  160.             exit(1);
  161.             };
  162.           token = gettok(0,white_space);
  163.           if ( token ) {
  164.             shortname = new char[strlen(token)+1]; 
  165.             strcpy(shortname,token);
  166.         }
  167.            else {
  168.             cerr << " Missing shortname for .facility\n";
  169.             exit(1);
  170.             };
  171.  
  172.           errfacility =
  173.               new ErrorFacility(longname,shortname,
  174.                   facility_code(longname),basename);
  175.           errfacility->h_init(*hstrm);
  176.           errfacility->h_define(*hstrm,"FACILITY",errfacility->code());
  177.           errorcount = (errfacility->code())<<FAC_SHIFT;
  178.            errfacility->h_define(*hstrm,"FIRSTERROR",errorcount);
  179.           };// end .facility
  180.  
  181.            if ( strcmp(token,".severity") == 0 ) { // start new severity list 
  182.           if ( !new_facility ) {
  183.         cerr << "errgen> missing facility definition\n";
  184.         exit(1);
  185.             };
  186.           new_severity = 1;                       
  187.  
  188.           token = gettok(0,white_space);
  189.           severity = severity_code(token);
  190.           };// end .severity 
  191.          break;
  192.        default:    // process error line 
  193.      if ( !new_severity ) {
  194.        cerr << "Missing severity definition\n";
  195.        exit(1);
  196.        };
  197.          token = gettok(buf,white_space);    // get error name token
  198.      cerr << token << " ";
  199.       errfacility->h_define(*hstrm,token,errorcount++);
  200.      token1 = gettok(0,"\n");        // get rest of line
  201.      if ( token1[0] == '"' ) {    // skip args field
  202.        token = "";
  203.        }
  204.       else {        
  205.        token = gettok(token1,white_space);     // get args field
  206.        token1 = gettok(0,"\n");        // get text field
  207.        };
  208.  
  209.          errfac = new ErrorSpecs(severity,token,token1);
  210.      errfacility->add(*errfac);
  211.          break;
  212.        };// end switch 
  213.      };// end while 
  214.  
  215.      cerr << " End of Stream.\n";
  216.      errfacility->h_define(*hstrm,"LAST_ERROR",--errorcount);
  217.      errfacility->h_finish(*hstrm);
  218.      errfacility->printOn(*cstrm); *cstrm << "\n";
  219. }/* end process_commands */
  220.  
  221. main(int argc,char* argv[])
  222. {
  223.   int iarg = 1;
  224.   while (argc > 1 && argv[iarg][0] == '-') {
  225.     switch (argv[iarg][1]) {
  226.     case 'r':        // error facility registry file directory
  227.     case 'R':
  228.       regdirname = &(argv[iarg])[2];
  229.       break;
  230.     default:
  231.       cerr << "unknown option " << argv[iarg] << "\n";
  232.       exit(1);
  233.     }
  234.     argc--;  iarg++;
  235.   }
  236.   if (argc < 2) {
  237.     cerr << "no errfiles specified\n";
  238.     exit(1);
  239.   };
  240.   while (--argc) {
  241.      make_filenames(argv[iarg++]);
  242.      cmdstrm = open_istream(cmdfile);
  243.      hstrm = open_ostream(hfile);
  244.      cstrm = open_ostream(cfile);
  245.      process_commands();
  246.      delete cmdstrm;
  247.      delete hstrm;
  248.      delete cstrm;
  249.      clean_filenames();
  250.    };
  251. exit(0);
  252. }/* end main */
  253.